/*
* SuctionCalController.java
* Created on  2015/7/21 10:29
* 版本       修改时间          作者      修改内容
* V1.0.1    2015/7/21       longshihui    初始版本
*
*/

package com.infitecs.eyas.suction;

import com.infitecs.eyas.expression.service.ExpressionService;
import com.infitecs.eyas.expression.service.ToolExpressionService;
import com.infitecs.eyas.result.service.MiddleResultService;
import com.infitecs.eyas.session.SessionService;
import com.infitecs.eyas.session.model.Param;
import com.infitecs.eyas.suction.model.SuctionCalculate;
import com.infitecs.eyas.suction.model.SuctionDes;
import com.infitecs.eyas.suction.model.SunctionCalcalatorBase;
import com.infitecs.eyas.suction.service.SuctionCalculatorServices;
import com.infitecs.eyas.suction.service.SuctionDesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 类的描述信息
 *
 * @author longshihui
 * @version 1.0.1
 */
@Controller
@RequestMapping("/suction/calculate")
public class SuctionCalController {

    @Autowired
    private SessionService sessionService;

    @Autowired
    private SuctionCalculatorServices suctionCalculatorServices;

    @Autowired
    private ExpressionService expressionService;

    @Autowired
    private ToolExpressionService toolExpressionService;

    @RequestMapping("")
    public String getPage(Model model) {
        try {
            sessionService.getCaseId();
            List<SuctionCalculate> suctionCalculates = suctionCalculatorServices.getSuctionCalculatorList();
            if (suctionCalculates == null || suctionCalculates.isEmpty()) {
                suctionCalculatorServices.insertSuctionCalculator();
            }
            SunctionCalcalatorBase sunctionCalcalatorBase = suctionCalculatorServices.getSunctionCalculator();
            if (sunctionCalcalatorBase == null) {
                suctionCalculatorServices.insertNewSuctionBase();
                sunctionCalcalatorBase = suctionCalculatorServices.getSunctionCalculator();
            }
            List<SuctionCalculate> in = suctionCalculatorServices.getIn();
            List<SuctionCalculate> out = suctionCalculatorServices.getOut();
            model.addAttribute("in", in);
            model.addAttribute("out", out);
            model.addAttribute("sunctionCalcalatorBase", sunctionCalcalatorBase);
            sessionService.setEnvironment('Y');
        } catch (Exception e) {
            return "suction/calculate";
        }
        return "suction/calculate";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public int add(SuctionCalculate suctionCalculate) {
        return suctionCalculatorServices.insertOneSuction(suctionCalculate);
    }

    @RequestMapping(value = "/del", method = RequestMethod.DELETE)
    @ResponseBody
    public void del(@RequestParam("id") int id) {
        suctionCalculatorServices.del(id);
    }

    @RequestMapping(value = "/editTitle", method = RequestMethod.POST)
    @ResponseBody
    public void editTitle(SuctionCalculate suctionCalculate) {
        suctionCalculatorServices.editTitle(suctionCalculate);
    }

    @RequestMapping(value = "/setE", method = RequestMethod.GET)
    @ResponseBody
    public void setEv() {
        sessionService.setEnvironment('Y');
    }

    @RequestMapping(value = "/getToolBarValues", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Double> getToolBarValues(@RequestParam("id") int id) {
        sessionService.setEnvironment('Y');
        return toolExpressionService.getResult();
    }

    @RequestMapping(value = "/autoCal", method = RequestMethod.POST)
    @ResponseBody
    public Double autoCal(SuctionCalculate suctionCalculate) {
        return suctionCalculate.getP() * suctionCalculate.getZ();
    }

    @RequestMapping(value = "/autoCalRowSum", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> autoCalSum(@RequestBody List<SuctionCalculate> suctionCalculates) {
//        E13和E14待取
        expressionService.calculate(new Param("YE13"));
        expressionService.calculate(new Param("YE14"));
        Double E13 = sessionService.getParamMap().get("YE13");
        Double E14 = sessionService.getParamMap().get("YE14");
        Double E17 = sessionService.getParamMap().get("YE17");
        double calculateLost = 0;
        double designLost = 0;
        double t;
        Iterator<SuctionCalculate> i = suctionCalculates.iterator();
        SuctionCalculate s;
        while (i.hasNext()) {
            s = i.next();
            t = s.getLost();
            if (s.getCheck1()) {
                if (E13 != null && E14 != null) {
                    t *= (1 + (E14 / E13 / 1000));
                } else {
                    return null;
                }
            }
            calculateLost += t;
            if (s.getCheck2()) {
                if (E17 != null) {
                    t *= (1 + E17 / 100);
                } else {
                    return null;
                }
            }
            designLost += t;
        }
        Map<String, String> result = new HashMap<>();
        result.put("calculateLost", Double.toString(calculateLost));
        result.put("designLost", Double.toString(designLost));
        this.updateSuctionRowSum(suctionCalculates.get(0), calculateLost, designLost);
        return result;
    }

    @RequestMapping(value = "/saveRow", method = RequestMethod.POST)
    @ResponseBody
    public void saveRow(SuctionCalculate suctionCalculate) {
        suctionCalculatorServices.update(suctionCalculate);
    }

    @RequestMapping(value = "/autoCalTableTotal", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> updateTotal(@RequestBody Map<String, Object> wrapper) {
        int source = Integer.parseInt(wrapper.get("source").toString());
        List<Map<String, String>> dataWrapper = (List<Map<String, String>>) wrapper.get("data");
        double calculateLostTotal = 0;
        double designLostTotal = 0;
        Map<String, String> data;
        Iterator<Map<String, String>> i = dataWrapper.iterator();
        while (i.hasNext()) {
            data = i.next();
            calculateLostTotal += Double.parseDouble(data.get("calculateLost"));
            designLostTotal += Double.parseDouble(data.get("designLost"));
        }
        Map<String, String> result = new HashMap<>();


        if (1 == source) {
            result.put("E32", Double.toString(calculateLostTotal));
            this.updateSuctionCalculator("E32", calculateLostTotal);
            result.put("E35", Double.toString(designLostTotal));
            this.updateSuctionCalculator("E35", designLostTotal);
        } else {
            result.put("E33", Double.toString(calculateLostTotal));
            this.updateSuctionCalculator("E33", calculateLostTotal);
            result.put("E36", Double.toString(designLostTotal));
            this.updateSuctionCalculator("E36", designLostTotal);
        }
        return result;
    }

    @RequestMapping(value = "/updateParam", method = RequestMethod.POST)
    @ResponseBody
    public void updateSuctionCalculator(@RequestParam String paramName, @RequestParam Double paramValue) {
        SunctionCalcalatorBase sunctionCalcalatorBase = new SunctionCalcalatorBase();
        sunctionCalcalatorBase.set(paramName, paramValue);
        sunctionCalcalatorBase.setCaseId(sessionService.getCaseId());
        suctionCalculatorServices.updateParam(sunctionCalcalatorBase);
    }

    private void updateSuctionRowSum(SuctionCalculate suctionCalculate, double RowCalculateLostSum, double RowDesignLostSum) {
        StringBuilder paramName = new StringBuilder();
        switch (suctionCalculate.getSmallType()) {
            case 1:
                paramName.append("pipe_");
                break;
            case 2:
                paramName.append("parts_");
                break;
            case 3:
                paramName.append("spare_");
                break;
            case 4:
                paramName.append("device_");
                break;
            case 5:
                paramName.append("other_");
                break;
            default:
                break;
        }
        if (suctionCalculate.getLagreType() == 1) {
            paramName.append("in_");
        } else {
            paramName.append("out_");
        }
        this.updateSuctionCalculator(paramName.toString() + "one", RowCalculateLostSum);
        this.updateSuctionCalculator(paramName.toString() + "two", RowDesignLostSum);
    }

    @RequestMapping(value = "/getParamInit", method = RequestMethod.POST)
    @ResponseBody
    public void getParamInit() {
        sessionService.putSuctionCalInit(sessionService.getCaseId());
    }
}
